/**
 * 
 */
package sailing.ui.base;

import java.awt.Component;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JDialog;

import org.jdesktop.application.SingleFrameApplication;

/**
 * @author kevin
 * 
 */
public abstract class AbstractJDialog extends JDialog {

	private final SingleFrameApplication singleFrameApplication;


	public AbstractJDialog(Window window, String title, ModalityType applicationModal,
			SingleFrameApplication singleFrameApplication) {
		super(window, title, applicationModal);
		this.singleFrameApplication = singleFrameApplication;
		this.initialUI();
		this.setToCenter();
	}


	protected abstract void initialUI();


	protected SingleFrameApplication getApplication() {
		return this.singleFrameApplication;
	}


	private void setToCenter() {
		Component owner = this.getOwner();
		if (owner != null) {
			this.setLocationRelativeTo(owner);
			return;
		}

		Rectangle screenRect = this.getGraphicsConfiguration().getBounds();
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());

		int centerWidth = screenRect.width < this.getSize().width ? screenRect.x : screenRect.x + screenRect.width / 2
				- this.getSize().width / 2;
		int centerHeight = screenRect.height < this.getSize().height ? screenRect.y : screenRect.y + screenRect.height
				/ 2 - this.getSize().height / 2;

		centerHeight = centerHeight < screenInsets.top ? screenInsets.top : centerHeight;

		this.setLocation(centerWidth, centerHeight);
	}

}
